The Image Compression Manager provides a rich set of functions that allow applications to compress images. Some of these functions present a straightforward interface that is suitable for applications that need little control over the compression operation. Others permit applications to control the parameters that govern the compression operation.
This section describes the basic steps that your application follows when compressing a single frame of image data. Following this discussion, Listing 1 shows a sample function that compresses an image.
First, determine the parameters for the compression operation. Typically, the user specifies these parameters in a user dialog box you may supply via the standard compression dialog component. For comprehensive details, see the chapter "Standard Image-Compression Dialog Components" in Inside Macintosh: QuickTime Components . Your application may choose to give the user the ability to specify such parameters as the compression algorithm, image quality, and so on.
Your application may give the user the option to specify a compression algorithm based on an important performance characteristic. For example, the user may be most concerned with size, speed, or quality. The Image Compression Manager allows your application to choose the compressor component that meets the specified criterion.
To determine the maximum size of the resulting compressed image, your application should then call the Image Compression Manager's GetMaxCompressionSize function (described on GetMaxCompressionSize ). You provide the specified compression parameters to this function. In response, the Image Compression Manager invokes the appropriate compressor component to determine the maximum number of bytes required to store the compressed image. Your application should then reserve sufficient memory to accommodate the compressed image or use a data-unloading function to spool the compressed data to disk (see "Spooling Compressed Data," for more information about data-unloading functions).
Once the user has specified the compression parameters and your application has established an appropriate environment for the operation, call the CompressImage (or FCompressImage ) function to compress the image. Use the CompressImage function (described on CompressImage ) if your application does not need to control all the parameters governing compression. If your application needs access to other compression parameters, use the FCompressImage function (described on FCompressImage ).
The Image Compression Manager manages the compression operation and invokes the appropriate compressor. The manager returns the compressed image and its associated image description structure to your application. Note that the image description structure contains a field indicating the size of the resulting image.
Note
You should use the standard compression dialog component to set up the parameters for compression. See the chapter "Standard Image-Compression Dialog Components" in
Inside Macintosh: QuickTime Components
for details.
Listing 1 Compressing and decompressing an image
#include <Types.h>
#include <Traps.h>
#include <Memory.h>
#include <Errors.h>
#include <FixMath.h>
#include "Movies.h"
#include "ImageCompression.h"
#include "StdCompression.h"
#define kMgrChoose 0
PicHandle GetQTCompressedPict (PixMapHandle myPixMap);
PicHandle GetQTCompressedPict( PixMapHandle myPixMap )
{
long maxCompressedSize = 0;
Handle compressedDataH = nil;
Ptr compressedDataP;
ImageDescriptionHandle imageDescH = nil;
OSErr theErr;
PicHandle myPic = nil;
Rect bounds = (**myPixMap).bounds;
CodecType theCodecType = 'jpeg';
CodecComponent theCodec = (CodecComponent)anyCodec;
CodecQ spatialQuality = codecNormalQuality;
short depth = 0;/* let ICM choose depth */
theErr = GetMaxCompressionSize( myPixMap, &bounds, depth,
spatialQuality, theCodecType,
(CompressorComponent)theCodec,
&maxCompressedSize);
if ( theErr ) return nil;
imageDescH = (ImageDescriptionHandle)NewHandle(4);
compressedDataH = NewHandle(maxCompressedSize);
if ( compressedDataH != nil && imageDescH != nil )
{
MoveHHi(compressedDataH);
HLock(compressedDataH);
compressedDataP = StripAddress(*compressedDataH);
theErr = CompressImage( myPixMap,
&bounds,
spatialQuality,
theCodecType,
imageDescH,
compressedDataP);
if ( theErr == noErr )
{
ClipRect(&bounds);
myPic = OpenPicture(&bounds);
theErr = DecompressImage( compressedDataP,
imageDescH,
myPixMap,
&bounds,
&bounds,
srcCopy,
nil );
ClosePicture();
}
if ( theErr
|| GetHandleSize((Handle)myPic) == sizeof(Picture) )
{
KillPicture(myPic);
myPic = nil;
}
}
if (imageDescH) DisposeHandle( (Handle)imageDescH);
if (compressedDataH) DisposeHandle( compressedDataH);
return myPic;
}